package com.dyb.demo.test_thread.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * park 阻塞当前线程
 * 与wait区别：1.wait/notify调用之前需要获取锁对象 2.park/unPark可以唤醒指定线程，wait/notify随即唤醒一个
 * @author dyb
 * @date 2020/7/18
 */
@SuppressWarnings("Duplicates")
public class TestPark {

    public static void main(String[] args) throws InterruptedException {
        TestPark TestPark = new TestPark();
        Thread t1 = new Thread(TestPark.new MyThread(), "t1");

        t1.start();
        Thread.sleep(1000);
        LockSupport.unpark(t1);
        System.out.println(Thread.currentThread().getName() + "任务执行结束");

    }

    class MyThread implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始执行");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "执行结束");
        }
    }
}
